package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.ManagementDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.exceptions.BusinessException;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Index;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * 健康干预CRUD操作业务实现类
 *
 * @author G.J.T
 */
@Service(version = "1.0.0", interfaceClass = ManagementService.class)
@Transactional
public class ManagementServiceImpl implements ManagementService {

    @Resource
    private ManagementDao managementDao;

    /**
     * 分页查询
     *
     * @param pageNum     查询页码
     * @param pageSize    每页显示条数
     * @param queryString 检查组编码、助记码或者名称
     * @return 分页查询返回结果
     */
    @Override
    public PageResult findPage(Integer pageNum, Integer pageSize, String queryString) {
        //开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        //调用dao层查询语句
        Page<Index> page = managementDao.findPage(queryString);
        //封装分页查询结果对象
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增检查套餐
     *
     * @param index 指标项实例
     */
    @Override
    public void add(Index index) throws BusinessException {
        Integer count = managementDao.findByIndicator(index);
        if (count > 0) {
            throw new BusinessException("已存在相同的指标名称，添加失败！");
        }
        //添加指标管理
        managementDao.save(index);
    }

    /**
     * 编辑
     *
     * @param index 指标项实体类
     * @return 编辑操作的结果
     */
    @Override
    public void update(Index index) throws BusinessException {
        Integer count = managementDao.findByNameIndicator(index);
        if (count > 0) {
            throw new RuntimeException("已存在同名或者同编号的指标名称，编辑失败！");
        }
        managementDao.update(index);
    }

    /**
     * 删除
     *
     * @param id 要删除的检查项的ID
     * @return 编辑操作的结果
     */
    @Override
    public void delete(int id) throws BusinessException {
        managementDao.delete(id);
    }
}
